/*
 * liense自己看着办吧，能在代码说明引用了此项目就不错了
 */
package com.kris;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查找更改的文件
 *
 * @author zhangqunshi
 */
public class ChangeFilesFinder {

    public static List<String> doFindChange(String filePath, long lastTime) throws FileNotFoundException {
        List<String> list = new ArrayList<>();

        File f = new File(filePath);
        if (!f.exists()) {
            throw new FileNotFoundException("File path doesn't exist!");
        }

        if (f.isDirectory()) {
            findAllChangeFiles(f, list, lastTime);
        } else if (isChanged(f, lastTime)) {
            list.add(f.getAbsolutePath());
        }
        return list;
    }

    private static void findAllChangeFiles(File dir, List<String> changeFiles, long time) {
        File[] fs = dir.listFiles();

        if (fs == null) {
            return;
        }

        for (File f : fs) {
            if (f.isDirectory()) {
                findAllChangeFiles(f, changeFiles, time);
            } else if (isChanged(f, time)) {
                String absPath = f.getAbsolutePath();
                changeFiles.add(absPath);
            }
        }

    }

    private static boolean isChanged(File f, long time) {
//        if (f.lastModified() > time) {
            System.out.println("file: " + f.getName() + " modify time: " + Utils.formatDate(f.lastModified())
                    + " against time: " + Utils.formatDate(time)
                    + " is changed: " + (f.lastModified() > time));
//        }
        return f.lastModified() > time;
    }

}
